package com.orangelife.common;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.orangelife.exception.AppBizException;
import com.orangelife.utils.HttpSvcUtils;

public abstract class MybaitsGenericDaoSubLogic extends SqlSessionDaoSupport {

	/* log initation */
	protected Logger logger = LoggerFactory
			.getLogger(MybaitsGenericDaoSubLogic.class);

	@Resource(name = "jdbcTemplate")
	public JdbcTemplate jdbcTemplate;   
//	@Resource(name = "sqlSessionFactoryBean")
//	public SqlSessionFactoryBean sqlSessionFactoryBean;
//	Map<String,SqlSessionTemplate> sqlSessions=new HashMap<String,SqlSessionTemplate>();
	@Resource(name = "sqlSession")
	public void setInjectSessionFactory(SqlSessionTemplate sessionFactory) {
	
	
		//SqlSessionFactoryBean nn=new SqlSessionFactoryBean();
		//nn.setMapperLocations(mapperLocations);
		
		super.setSqlSessionTemplate(sessionFactory);

	}
	

	public SqlSession getSqlSession(String id) throws AppBizException{
		
//		JSONObject 
		JSONObject	 jj=	HttpSvcUtils.doGetSvc("http://localhost:8080/PropFeeCenter/app/manage/openInfo/"+id,null);
		
		
		
		
		String errCode=jj.getString("errCode");
		String dataSourceUri="";
		
		if("0".equals(errCode)){
			
			JSONObject entity=jj.getJSONObject("entity");	
			dataSourceUri=entity.getString("dataSourceUri");
			
		}else{
			
			
			throw new AppBizException(99, "连接数据库失败");
		}
	
		 URI uri;
		
				try {
					uri = new URI("mysql://root:tobaby@localhost:3306/sss?autoReconnect=true&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf-8#ppp");
				} catch (URISyntaxException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		
		
		
		return this.getSqlSession();
//		SqlSessionTemplate s=null;
//		try {
//			
//			
//			if(sqlSessions.get(id)!=null){
//				
//				
//				
//				return sqlSessions.get(id);
//			}
//			
//			BasicDataSource b=new BasicDataSource();
//			b.setUrl("jdbc:mysql://developer.17orange.com:13306/"+id+"?autoReconnect=true&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf-8");
//			b.setPassword("tobaby");
//			b.setUsername("root");
//			
//			
//			b.setDriverClassName("com.mysql.jdbc.Driver");
////			<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource "
////					destroy-method="close">
////					<property name="driverClassName" value="${jdbc.driver}" />
////					<property name="url" value="${jdbc.url}" />
////					<property name="username" value="${jdbc.username}" />
////					<property name="password" value="${jdbc.password}" />
////					<property name="validationQuery">
////						<value>SELECT 1 FROM dual</value>
////					</property>
////
////					<!-- Connection Pooling Info -->
////					<property name="maxActive" value="30" />
////					<property name="maxIdle" value="30" />
////					<property name="defaultAutoCommit" value="true" />
////					<!-- 连接Idle一个小时后超时 -->
////					<property name="timeBetweenEvictionRunsMillis" value="3600000" />
////					<property name="minEvictableIdleTimeMillis" value="3600000" />
////				</bean>
//	
//			sqlSessionFactoryBean.setDataSource(b);
////			System.out.println(sqlSessionFactoryBean.);
//			
//			SqlSessionFactoryBean bb=new SqlSessionFactoryBean();
//			
//		
//			s=new SqlSessionTemplate(sqlSessionFactoryBean.getObjectAlways());
//			
//			sqlSessions.put(id,s);
//			System.out.println(s);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		
//		return s;
		
	}
	
	

	/**
	 * 根据XMLID进行查询
	 * */

	public List<Map<String, Object>> selectDataListByXmlID(String xmlID,
			Map<String, Object> params, int pageIndex, int pageSize)
			throws AppBizException {

		PageHelper.startPage(pageIndex, pageSize);

		List<Map<String, Object>> list = this.getSqlSession().selectList(xmlID,
				params);

		return list;
	}

	public List<Object> selectCommonDataListByXmlID(String xmlID,
			Map<String, Object> params, int pageIndex, int pageSize)
			throws AppBizException {

		PageHelper.startPage(pageIndex, pageSize);

		List<Object> list = this.getSqlSession().selectList(xmlID, params);

		return list;
	}

}
